#!/bin/sh
#
# see: dh_installdeb(1)

set -e

package="bonescript"

npm_pkg_install () {
	echo "${package}:installing:[${npm_file}]"
	if [ ! -d /usr/local/lib/node_modules/ ] ; then
		mkdir -p /usr/local/lib/node_modules/ || true
	fi
	if [ -d /usr/local/lib/node_modules/${npm_project}/ ] ; then
		rm -rf /usr/local/lib/node_modules/${npm_project}/ || true
	fi

	echo "${package}:updating:[/usr/local/lib/node_modules/${npm_project}/]"
	tar xf /var/cache/${package}/${npm_file} -C /usr/local/lib/node_modules/
}

npm_install () {
	node_bin="/usr/bin/nodejs"

	unset node_version
	node_version=$(/usr/bin/nodejs --version || true)
	echo "${package}:nodejs:[${node_version}]"

v6="v6.17.0"
v8="v8.16.1"

datestamp="20190903"

bonescript="bonescript-0.7.3-26c5522"
   winston="winston-2.1.1"

	case "${node_version}" in
	v6.*)
		npm_project="bonescript"
		npm_file="${bonescript}-${v6}-${datestamp}.tar.xz"
		npm_pkg_install

		npm_project="winston"
		npm_file="${winston}-${v6}.tar.xz"
		npm_pkg_install
		;;
	v8.*)
		npm_project="bonescript"
		npm_file="${bonescript}-${v8}-${datestamp}.tar.xz"
		npm_pkg_install

		npm_project="winston"
		npm_file="${winston}-${v8}.tar.xz"
		npm_pkg_install
		;;
	esac
}

npm_install

echo "NODE_PATH=/usr/local/lib/node_modules" > /etc/default/node
echo "export NODE_PATH=/usr/local/lib/node_modules" > /etc/profile.d/node.sh
chmod 755 /etc/profile.d/node.sh

systemctl enable bonescript.socket || true
systemctl enable bonescript-autorun.service || true

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

